﻿using System;
using System.Windows.Forms;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Data;
using InvoiceManagement.Support;
using System.Data.Common;
using System.Collections.Generic;

namespace InvoiceManagement.Forms
{
  public partial class EmployeeDetails : Form
  {
    private string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

    public EmployeeDetails()
    {
      InitializeComponent();
      //this.connection = new MySqlConnection(this.connectionString);
      DatabaseOperations.Initialize(this.connectionString);
    }

    private void OnCancelClicked(object sender, EventArgs e)
    {
      this.Close();
    }

    private void OnAddEntryClicked(object sender, EventArgs e)
    {
      try
      {
        string query = "INSERT INTO EmployeeDetails(EmployeeName,EmployeeNumber,MobileNumber,Qualification,Designation,Address) VALUES(@EmployeeName,@EmployeeNumber,@MobileNumber,@Qualification,@Designation,@Address)";
        List<MySqlParameter> parameters = new List<MySqlParameter>();
        parameters.Add(new MySqlParameter("@EmployeeName", this.txtEmployeeName.Text));
        parameters.Add(new MySqlParameter("@EmployeeNumber", this.txtEmployeeID.Text));
        parameters.Add(new MySqlParameter("@MobileNumber", this.txtMobileNumber.Text));
        parameters.Add(new MySqlParameter("@Qualification", this.txtQualification.Text));
        parameters.Add(new MySqlParameter("@Designation", this.txtDesignation.Text));
        parameters.Add(new MySqlParameter("@Address", this.txtAddress.Text));
        DatabaseOperations.ExecuteQueryWithParameters(query, parameters);

        this.DialogResult = DialogResult.OK;
        this.Close();
      }
      catch (MySqlException ex)
      {
        Console.WriteLine("Error: {0}", ex.ToString());
        MessageBox.Show("Invalid Entry", "Inventory Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }
  }
}
